# -*- coding: utf-8 -*-
# @Time    : 2024/4/23 15:10
# @Author  : floraachy
# @File    : repo_new_pull_request_page.py
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoEditPullRequestPage(BasePage):
    """项目-编辑合并请求页面"""
    # 合并请求标题输入框
    locator_pr_title_input = "//input[@id='NewMergeForm_title']"
    # 合并请求内容输入框
    locator_pr_content_input = "//textarea/following::div[contains(@class, 'CodeMirror-wrap')]"
    # 审查人员
    locator_assigner_div = "//div[@id='NewMergeForm_assigned_to_id']"
    locator_assigner_name_div = "//li[text()='未选择审查人员']/following-sibling::li[text()='{}']"
    # 里程碑
    locator_milestone_div = "//div[@id='NewMergeForm_fixed_version_id']"
    locator_milestone_name_div = "//li[text()='未选择里程碑']/following-sibling::li[text()='{}']"
    # 标记
    locator_label_div = "//div[@id='NewMergeForm_issue_tag_ids']"
    locator_label_name_div = "//li[text()='未选择标记']/following-sibling::li"
    # 优先级
    locator_priority_div = "//div[@id='NewMergeForm_priority_id']"
    locator_priority_name_div = "//li[text()='{}']"
    # 提交 按钮
    locator_submit_pr_btn = "//span[text()='提 交']/parent::button"

    @allure.step("访问编辑合并请求页：/{repo_owner}/{repo_identifier}/pulls/{pull_request_id}/edit")
    def navigate(self, repo_owner, repo_identifier, pull_request_id):
        """
        访问新建合并请求页
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/pulls/{pull_request_id}/edit")

    @allure.step("输入合并请求标题： {pr_title}")
    def input_pull_request_title(self, pr_title):
        """
        输入合并请求标题
        """
        self.input(self.locator_pr_title_input, pr_title)

    @allure.step("输入合并请求内容： {pr_content}")
    def input_pull_request_content(self, pr_content):
        """
        输入合并请求内容
        """
        self.page.wait_for_timeout(1000)
        elem = self.page.query_selector(selector=self.locator_pr_content_input)
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, pr_content])

    @allure.step("选择审查人员： {assigner}")
    def choose_assigner(self, assigner):
        """
        选择审查人员
        """
        self.click(self.locator_assigner_div.format(assigner))
        self.click(self.locator_assigner_name_div.format(assigner))

    @allure.step("选择里程碑： {milestone}")
    def choose_milestone(self, milestone):
        """
        选择里程碑
        """
        self.click(self.locator_milestone_div)
        self.click(self.locator_milestone_name_div.format(milestone))

    @allure.step("选择标记： {label}")
    def choose_label(self, label):
        """
        选择标记
        """
        self.click(self.locator_label_div)
        self.click(self.locator_label_name_div.format(label))

    @allure.step("选择优先级： {priority}")
    def choose_priority(self, priority):
        """
        选择优先级
        """
        self.click(self.locator_priority_div)
        self.click(self.locator_priority_name_div.format(priority))

    @allure.step("点击【提交】按钮")
    def click_submit_button(self):
        """
        点击“提交”按钮
        """
        self.click(self.locator_submit_pr_btn)

    # --------------------- 流程 -------------------------------------
    @allure.step(
        "编辑合并请求，  合并请求标题：{pr_title}， 合并请求内容： {pr_content}")
    def edit_pull_request_flow(self, pr_title, pr_content=None, pr_assigner=None, pr_milestone=None,
                              pr_label=None,
                              pr_priority=None):
        """
        合并请求(PR)详情页面，编辑合并请求
        """
        # 输入合并请求标题：new_${generate_name()}
        self.input_pull_request_title(pr_title)
        # 输入合并请求内容：${generate_paragraph()}
        if pr_content:
            self.input_pull_request_content(pr_content)
        # 选择审查人员
        if pr_assigner:
            self.choose_assigner(pr_assigner)
        # 选择里程碑
        if pr_milestone:
            self.choose_milestone(pr_milestone)
        # 选择标记
        if pr_label:
            self.choose_label(pr_label)
        # 选择优先级
        if pr_priority:
            self.choose_priority(pr_priority)
        # 点击【提交】按钮
        self.click_submit_button()
